import scrapy
from myscrapy.items import MyscrapyItem

class DangSpider(scrapy.Spider):
    name = "dang"
    # allowed_domains = ["category.dangdang.com"]
    #1.修改start_urls
    start_urls = ["https://category.dangdang.com/cp01.34.04.00.00.00.html"]
    #解析响应
    def parse(self, response):
        book_nodes = response.xpath('//ul[@class="bigimg"]/li')
        for book in book_nodes:
            info = MyscrapyItem()
            info["title"] = book.xpath('./p[1]/a/@title').extract_first()
            info["price"] = book.xpath('./p[3]/span[1]/text()').extract_first()
            yield info
        
        next_page = response.xpath('//li[@class="next"]/a/@href').extract_first()
        if next_page is not None:
            yield response.follow(next_page, self.parse)

